{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 183,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://q.stock.sohu.com/hisHq?code=cn_000725&start=20100120&end=20200729\n",
      "https://q.stock.sohu.com/hisHq?code=zs_000001&start=20100120&end=20200729\n"
     ]
    }
   ],
   "source": [
    "import requests as req\n",
    "import pandas as pd\n",
    "import datetime\n",
    "import json\n",
    "befordaycount = 180\n",
    "code = \"cn_000725\"\n",
    "startTime = 20100120\n",
    "endTime = 20200729\n",
    "def getZSData(start, end):\n",
    "    url = \"https://q.stock.sohu.com/hisHq?code=zs_000001&start={}&end={}\".format(start, end)\n",
    "    print(url)\n",
    "    res = req.get(url)\n",
    "    return json.loads(res.text)\n",
    "def getData(code, start, end):\n",
    "    url = \"https://q.stock.sohu.com/hisHq?code={}&start={}&end={}\".format(code, start, end)\n",
    "    print(url)\n",
    "    res = req.get(url)\n",
    "#     print(res)\n",
    "    return json.loads(res.text)\n",
    "data = getData(code, startTime, endTime)[0]['hq']\n",
    "zsData = getZSData(startTime, endTime)[0]['hq']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 189,
   "metadata": {},
   "outputs": [],
   "source": [
    "def convertWeek(dt):\n",
    "    week=datetime.datetime.strptime(dt, \"%Y-%m-%d\").weekday()\n",
    "    return week\n",
    "def convertToFloat(num):\n",
    "    num = num.replace(\"%\", \"\")\n",
    "    if num != \"-\":\n",
    "        num = float(num) + 10\n",
    "    else:\n",
    "        num = 10\n",
    "    return num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 190,
   "metadata": {},
   "outputs": [],
   "source": [
    "def getBeforData(index, list, beforCount):\n",
    "    data = []\n",
    "    for i in range(index, index + beforCount):\n",
    "        if i < len(list):\n",
    "            data.append(list[i])\n",
    "    return data\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 191,
   "metadata": {},
   "outputs": [],
   "source": [
    "def connectData(beforData,fiveZSData, currentData):\n",
    "    list = []\n",
    "    for data in beforData:\n",
    "#         for index in range(1, len(data)):\n",
    "#             list.append(convertToFloat(data[index]))\n",
    "        list.append(convertToFloat(data[4]))\n",
    "        list.append(convertToFloat(data[9]))\n",
    "#         list.append(data)\n",
    "    for data in fiveZSData:\n",
    "#          for index in range(1, len(data) - 1):\n",
    "#             list.append(convertToFloat(data[index]))\n",
    "        list.append(convertToFloat(data[4]))\n",
    "    list.append(currentData)\n",
    "    return list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 192,
   "metadata": {},
   "outputs": [],
   "source": [
    "allData = []\n",
    "indexList = []\n",
    "valuesLength = 0\n",
    "for  index in range(1, len(data)):\n",
    "    hq= data[index]\n",
    "#     week = convertWeek(hq[0])\n",
    "#     if week ==0:\n",
    "    fiveData = getBeforData(index + 1, data, befordaycount)\n",
    "    fiveZSData = getBeforData(index + 1, zsData,befordaycount)\n",
    "    if len(fiveData) ==befordaycount:\n",
    "#         indexList.append(hq[0])\n",
    "        \n",
    "        d = connectData(fiveData, fiveZSData,convertToFloat(hq[4]))\n",
    "        valuesLength = len(d)\n",
    "        allData.append(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 188,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "541\n",
      "['num0', 'num1', 'num2', 'num3', 'num4', 'num5', 'num6', 'num7', 'num8', 'num9', 'num10', 'num11', 'num12', 'num13', 'num14', 'num15', 'num16', 'num17', 'num18', 'num19', 'num20', 'num21', 'num22', 'num23', 'num24', 'num25', 'num26', 'num27', 'num28', 'num29', 'num30', 'num31', 'num32', 'num33', 'num34', 'num35', 'num36', 'num37', 'num38', 'num39', 'num40', 'num41', 'num42', 'num43', 'num44', 'num45', 'num46', 'num47', 'num48', 'num49', 'num50', 'num51', 'num52', 'num53', 'num54', 'num55', 'num56', 'num57', 'num58', 'num59', 'num60', 'num61', 'num62', 'num63', 'num64', 'num65', 'num66', 'num67', 'num68', 'num69', 'num70', 'num71', 'num72', 'num73', 'num74', 'num75', 'num76', 'num77', 'num78', 'num79', 'num80', 'num81', 'num82', 'num83', 'num84', 'num85', 'num86', 'num87', 'num88', 'num89', 'num90', 'num91', 'num92', 'num93', 'num94', 'num95', 'num96', 'num97', 'num98', 'num99', 'num100', 'num101', 'num102', 'num103', 'num104', 'num105', 'num106', 'num107', 'num108', 'num109', 'num110', 'num111', 'num112', 'num113', 'num114', 'num115', 'num116', 'num117', 'num118', 'num119', 'num120', 'num121', 'num122', 'num123', 'num124', 'num125', 'num126', 'num127', 'num128', 'num129', 'num130', 'num131', 'num132', 'num133', 'num134', 'num135', 'num136', 'num137', 'num138', 'num139', 'num140', 'num141', 'num142', 'num143', 'num144', 'num145', 'num146', 'num147', 'num148', 'num149', 'num150', 'num151', 'num152', 'num153', 'num154', 'num155', 'num156', 'num157', 'num158', 'num159', 'num160', 'num161', 'num162', 'num163', 'num164', 'num165', 'num166', 'num167', 'num168', 'num169', 'num170', 'num171', 'num172', 'num173', 'num174', 'num175', 'num176', 'num177', 'num178', 'num179', 'num180', 'num181', 'num182', 'num183', 'num184', 'num185', 'num186', 'num187', 'num188', 'num189', 'num190', 'num191', 'num192', 'num193', 'num194', 'num195', 'num196', 'num197', 'num198', 'num199', 'num200', 'num201', 'num202', 'num203', 'num204', 'num205', 'num206', 'num207', 'num208', 'num209', 'num210', 'num211', 'num212', 'num213', 'num214', 'num215', 'num216', 'num217', 'num218', 'num219', 'num220', 'num221', 'num222', 'num223', 'num224', 'num225', 'num226', 'num227', 'num228', 'num229', 'num230', 'num231', 'num232', 'num233', 'num234', 'num235', 'num236', 'num237', 'num238', 'num239', 'num240', 'num241', 'num242', 'num243', 'num244', 'num245', 'num246', 'num247', 'num248', 'num249', 'num250', 'num251', 'num252', 'num253', 'num254', 'num255', 'num256', 'num257', 'num258', 'num259', 'num260', 'num261', 'num262', 'num263', 'num264', 'num265', 'num266', 'num267', 'num268', 'num269', 'num270', 'num271', 'num272', 'num273', 'num274', 'num275', 'num276', 'num277', 'num278', 'num279', 'num280', 'num281', 'num282', 'num283', 'num284', 'num285', 'num286', 'num287', 'num288', 'num289', 'num290', 'num291', 'num292', 'num293', 'num294', 'num295', 'num296', 'num297', 'num298', 'num299', 'num300', 'num301', 'num302', 'num303', 'num304', 'num305', 'num306', 'num307', 'num308', 'num309', 'num310', 'num311', 'num312', 'num313', 'num314', 'num315', 'num316', 'num317', 'num318', 'num319', 'num320', 'num321', 'num322', 'num323', 'num324', 'num325', 'num326', 'num327', 'num328', 'num329', 'num330', 'num331', 'num332', 'num333', 'num334', 'num335', 'num336', 'num337', 'num338', 'num339', 'num340', 'num341', 'num342', 'num343', 'num344', 'num345', 'num346', 'num347', 'num348', 'num349', 'num350', 'num351', 'num352', 'num353', 'num354', 'num355', 'num356', 'num357', 'num358', 'num359', 'num360', 'num361', 'num362', 'num363', 'num364', 'num365', 'num366', 'num367', 'num368', 'num369', 'num370', 'num371', 'num372', 'num373', 'num374', 'num375', 'num376', 'num377', 'num378', 'num379', 'num380', 'num381', 'num382', 'num383', 'num384', 'num385', 'num386', 'num387', 'num388', 'num389', 'num390', 'num391', 'num392', 'num393', 'num394', 'num395', 'num396', 'num397', 'num398', 'num399', 'num400', 'num401', 'num402', 'num403', 'num404', 'num405', 'num406', 'num407', 'num408', 'num409', 'num410', 'num411', 'num412', 'num413', 'num414', 'num415', 'num416', 'num417', 'num418', 'num419', 'num420', 'num421', 'num422', 'num423', 'num424', 'num425', 'num426', 'num427', 'num428', 'num429', 'num430', 'num431', 'num432', 'num433', 'num434', 'num435', 'num436', 'num437', 'num438', 'num439', 'num440', 'num441', 'num442', 'num443', 'num444', 'num445', 'num446', 'num447', 'num448', 'num449', 'num450', 'num451', 'num452', 'num453', 'num454', 'num455', 'num456', 'num457', 'num458', 'num459', 'num460', 'num461', 'num462', 'num463', 'num464', 'num465', 'num466', 'num467', 'num468', 'num469', 'num470', 'num471', 'num472', 'num473', 'num474', 'num475', 'num476', 'num477', 'num478', 'num479', 'num480', 'num481', 'num482', 'num483', 'num484', 'num485', 'num486', 'num487', 'num488', 'num489', 'num490', 'num491', 'num492', 'num493', 'num494', 'num495', 'num496', 'num497', 'num498', 'num499', 'num500', 'num501', 'num502', 'num503', 'num504', 'num505', 'num506', 'num507', 'num508', 'num509', 'num510', 'num511', 'num512', 'num513', 'num514', 'num515', 'num516', 'num517', 'num518', 'num519', 'num520', 'num521', 'num522', 'num523', 'num524', 'num525', 'num526', 'num527', 'num528', 'num529', 'num530', 'num531', 'num532', 'num533', 'num534', 'num535', 'num536', 'num537', 'num538', 'num539', 'num540']\n"
     ]
    }
   ],
   "source": [
    "print(valuesLength)\n",
    "columns = []\n",
    "for index in range(0,valuesLength):\n",
    "    columns.append(\"num{}\".format(index))\n",
    "print(columns)\n",
    "csvdata = pd.DataFrame(columns = columns, data=allData)\n",
    "csvdata.to_csv(\"{}_data.csv\".format(code))\n",
    "\n",
    "currentData = []\n",
    "\n",
    "currentFiveData = getBeforData(0, data, befordaycount)\n",
    "currentFiveZSData = getBeforData(0, zsData,befordaycount)\n",
    "d = connectData(currentFiveData, currentFiveZSData, 0)\n",
    "# valuesLength = len(d)\n",
    "currentData.append(d)\n",
    "currentData_csv = pd.DataFrame(columns = columns, data = currentData)\n",
    "currentData_csv.to_csv(\"{}_data_test.csv\".format(code));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
